import { camera, scene, renderer } from './utils/init'
import * as THREE from 'three'


// 产生阴影的几个步骤
// 渲染器开启阴影
// 灯光产生阴影
// 物体产生阴影
// 平面接受阴影
function initBase() {
  const geometry = new THREE.PlaneGeometry(10, 10)
  const meterial = new THREE.MeshStandardMaterial({color: 0xffffff})
  const plane = new THREE.Mesh(geometry, meterial)
  plane.rotation.set(-Math.PI / 2, 0, 0)
  // 平面接受阴影
  plane.receiveShadow = true
  scene.add(plane)
}

function createLight () {
  const spotlight = new THREE.SpotLight(0xffffff, 1)
  spotlight.position.set(3, 3, 3)
  // 聚光灯开启阴影
  spotlight.castShadow = true
  scene.add(spotlight)
  const helper = new THREE.DirectionalLightHelper(spotlight, 1)
  // 让聚光灯打出阴影贴图的大小
  spotlight.shadow.mapSize.width = 4096
  spotlight.shadow.mapSize.height = 4096
  console.log(spotlight.shadow, 'spotlight.shadow');
  scene.add(helper)
}

// 平行光
// function createLight () {
//   const directionalLight = new THREE.DirectionalLight(0xffffff, 1)
//   directionalLight.position.set(3, 3, 3)
//   scene.add(directionalLight)
//   // 平行光接受阴影
//   directionalLight.castShadow = true
//   const helper = new THREE.DirectionalLightHelper(directionalLight, 1)
//   scene.add(helper)
// }

// 点光源
// function createLight () {
//   // 创建点光源对象 1是光照强度 100是光源照射的最大距离
//   const light = new THREE.PointLight(0xffff00, 1, 100)
//   light.position.set(3, 3, 3)
//   light.castShadow = true
//   scene.add(light)
//   // 创建点光源辅助对象 试下来感觉差别不大
//   const pointLightHelper = new THREE.PointLightHelper(light, 1)
//   scene.add(pointLightHelper)
// }
// 创建一个物体
function createSphere (){
  const sphere = new THREE.SphereGeometry(1, 32, 16)
  const material = new THREE.MeshStandardMaterial( { color: 0x00ff00 } )
  const mesh = new THREE.Mesh(sphere, material)
  mesh.position.set(0, 1, 0)
  // 物体产生阴影
  mesh.castShadow = true
  scene.add(mesh)
}
camera.position.set(5, 5, 5)
// 渲染器接受阴影
renderer.shadowMap.enabled = true
initBase()
createLight()
createSphere()
